/**
 * 
 */
package org.bookie.init.tasks;

import java.util.Properties;

import org.apache.hivemind.Registry;
import org.bookie.init.InitTask;
import org.bookie.model.Category;
import org.bookie.model.services.CategoryDAO;

/**
 * Loads data
 * 
 * @author zubairov
 * 
 */
public class CategoryLoad implements InitTask {
	private String[] categories = new String[]{
			 "Soccer", "Boxing", "Basketball", "Formula 1",
			 "Ice Hockey", "American Football", "Volleyball"
	};

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.bookie.init.InitTask#check(org.apache.hivemind.Registry)
	 */
	public Properties check(Registry registry) throws Exception {
		Properties result = new Properties();
		CategoryDAO dao = (CategoryDAO) registry.getService(CategoryDAO.class);
		for (String cat : categories) {
			boolean found = (dao.findByName(cat) != null);
			result.put(cat+" category exists", String.valueOf(found));
		}
		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.bookie.init.InitTask#execute(org.apache.hivemind.Registry)
	 */
	public Properties execute(Registry registry) throws Exception {
		Properties result = new Properties();
		CategoryDAO dao = (CategoryDAO) registry.getService(CategoryDAO.class);
		int i=0;
		for (String cat : categories) {
			boolean found = (dao.findByName(cat) != null);
			if(!found){
				Category category = dao.makePersistent(new Category(cat));
				result.put("Added category "+(i++),category.getName());
			}
		}
		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.bookie.init.InitTask#getTitle()
	 */
	public String getTitle() {
		return "Categories initialization";
	}

}
